package emulator.shell.GUI.menu;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JSplitPane;

import emulator.GUI.io.Via6522panel;
import emulator.hardware.io.Via6522;
import emulator.hardware.io.porthistory.PortStateHistory;
import emulator.hardware.nmos6502.Cpu6502;
import emulator.shell.GUI.graph.BitGraph;
import emulator.shell.GUI.windows.ManagedWindow;
import emulator.shell.GUI.windows.WindowManager;

public class WindowVia extends GraphWindow implements ActionListener
{
	private WindowManager windowManager;
	private int windowId;
	Via6522 via;
	private ManagedWindow window;
	private String title;
	private Via6522panel panelVia;
	PortStateHistory portStateHistory;
	
	public WindowVia(WindowManager window_manager, int window_id, String title, Via6522 via, Cpu6502 cpu, PortStateHistory portStateHistory)
	{
		this.windowManager = window_manager;
		this.windowId = window_id;
		this.title = title;
		this.via = via;
		this.cpu = cpu;
		this.portStateHistory = portStateHistory;
		via.getId();
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (windowManager.isWindowActive(windowId))
			closeWindow();
		else
			openWindow();
	}

	private void openWindow()
	{
		window = windowManager.createWindow(windowId, title);
		panelVia = new Via6522panel(via, title);
		if (portStateHistory != null)
		{
			JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelVia, createGraphFrame());
		    splitPane.setOneTouchExpandable(true);
		    splitPane.setDividerLocation(panelVia.getPreferredSize().width);
			
			window.getContentPane().add(splitPane);
		}
		else
			window.getContentPane().add(panelVia);
		window.restorePosition();
		window.setVisible(true);
		panelVia.updateFromVia();
		cpu.addRunningStateObserver(cpuObserver);
	}

	private void closeWindow()
	{
		cpu.deleteRunningStateObserver(cpuObserver);
		panelVia = null;
		windowManager.closeWindow(windowId);
	}

	@Override
	protected BitGraph createGraph() 
	{
		BitGraph bitGraph = new BitGraph(36);
		for (int i = 0; i < 8; i++)
		{
			bitGraph.getGraphLine(2*i).setLabel("PA"+i+"i").setStateHistory(portStateHistory.getPortAin(i)).setPositionIndex(i).setColor(Color.GRAY);
			bitGraph.getGraphLine(2*i+1).setLabel("PA"+i+"o").setStateHistory(portStateHistory.getPortAout(i)).setPositionIndex(i);
			bitGraph.getGraphLine(2*i+18).setLabel("PB"+i+"i").setStateHistory(portStateHistory.getPortBin(i)).setPositionIndex(i+10).setColor(Color.GRAY);
			bitGraph.getGraphLine(2*i+19).setLabel("PB"+i+"o").setStateHistory(portStateHistory.getPortBout(i)).setPositionIndex(i+10);
		}
		for (int i = 0; i < 2; i++)
		{
			bitGraph.getGraphLine(16+i).setLabel("CA"+(i+1)).setStateHistory(portStateHistory.getPortActrl(i)).setPositionIndex(8+i);
			bitGraph.getGraphLine(34+i).setLabel("CB"+(i+1)).setStateHistory(portStateHistory.getPortBctrl(i)).setPositionIndex(18+i);
		}
		return bitGraph;
	}

	@Override
	protected void onCpuStoppped()
	{
		panelVia.updateFromVia();
	}

	@Override
	protected long getMaxTicks()
	{
		return via.getTicks();
	}
}
